package com.juhuixing.common.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.juhuixing.common.exception.BusinessException;
import com.juhuixing.common.enums.SexEnum;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.util.Objects;

/**
 *@ClassName SexSerializer
 *@Description TODO
 *@Author 刘猛
 *@Date 2019/3/20 14:08
 **/
public class SexSerializer extends JsonSerializer<Integer> {
    @Override
    public void serialize(Integer sex, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        try {
            String sexRs;
            if(Objects.equals(SexEnum.MAN.getCode(),sex)){
                sexRs = SexEnum.MAN.getDesc();
            }else if(Objects.equals(SexEnum.WOMAN.getCode(),sex)){
                sexRs = SexEnum.WOMAN.getDesc();
            }else{
                sexRs = "未知";
            }
            jsonGenerator.writeString(sexRs);
        } catch (BusinessException e) {
            jsonGenerator.writeString(StringUtils.EMPTY);
        }
    }
}
